package com.hupu.jrs.data.local

import com.hupu.jrs.data.ContentDataSource
import com.hupu.jrs.util.HtmlUtil
import javax.inject.Inject
import rx.Observable
import rx.schedulers.Schedulers

/**
 * Created by Stark.Tony on 2018/6/3.
 */
class ContentLocalDataSource @Inject
constructor(private val mThreadInfoDao: ThreadInfoDao, private val mThreadReplyDao: ThreadReplyDao) : ContentDataSource {

    override fun getThreadInfo(fid: String, tid: String): Observable<ThreadInfo> {
        return Observable.create(Observable.OnSubscribe<ThreadInfo> { subscriber ->
            val threadInfos = mThreadInfoDao.queryBuilder().where(ThreadInfoDao.Properties.Tid.eq(tid)).list()
            if (!threadInfos.isEmpty()) {
                val threadInfo = threadInfos.get(0)
                val content = threadInfo.content
                threadInfo.content = HtmlUtil.transImgToLocal(content!!)
                subscriber.onNext(threadInfo)
            } else {
                subscriber.onNext(null)
            }
            subscriber.onCompleted()
        }).subscribeOn(Schedulers.io())
    }

    fun saveThreadInfo(mThreadInfo: ThreadInfo) {
        mThreadInfoDao.queryBuilder()
                .where(ThreadInfoDao.Properties.Tid.eq(mThreadInfo.tid))
                .buildDelete()
                .executeDeleteWithoutDetachingEntities()
        mThreadInfoDao.insert(mThreadInfo)
    }

    override fun getReplies(fid: String, tid: String, page: Int): Observable<List<ThreadReply>> {
        return Observable.create(Observable.OnSubscribe<List<ThreadReply>> { subscriber ->
            val replies = mThreadReplyDao.queryBuilder()
                    .where(ThreadReplyDao.Properties.Tid.eq(tid),
                            ThreadReplyDao.Properties.IsLight.eq(false),
                            ThreadReplyDao.Properties.PageIndex.eq(page))
                    .orderAsc(ThreadReplyDao.Properties.Floor)
                    .list()
            if (replies.isEmpty() && page > 1) {
                subscriber.onNext(null)
            } else {
                subscriber.onNext(replies)
            }
            subscriber.onCompleted()
        }).subscribeOn(Schedulers.io())
    }

    override fun getLightReplies(fid: String, tid: String): Observable<List<ThreadReply>> {
        return Observable.create(Observable.OnSubscribe<List<ThreadReply>> { subscriber ->
            val replies = mThreadReplyDao.queryBuilder()
                    .where(ThreadReplyDao.Properties.Tid.eq(tid),
                            ThreadReplyDao.Properties.IsLight.eq(true))
                    .orderDesc(ThreadReplyDao.Properties.Light_count)
                    .list()
            subscriber.onNext(replies)
            subscriber.onCompleted()
        }).subscribeOn(Schedulers.io())
    }

    fun saveThreadReply(reply: ThreadReply) {
        mThreadReplyDao.queryBuilder()
                .where(ThreadReplyDao.Properties.Pid.eq(reply.pid),
                        ThreadReplyDao.Properties.IsLight.eq(reply.isLight))
                .buildDelete()
                .executeDeleteWithoutDetachingEntities()
        mThreadReplyDao.insert(reply)
    }
}
